<template>
  <div>
    <h1>登录组件</h1>

    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="ruleForm"
    >
      <el-form-item label="用户名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pwd">
        <el-input v-model="ruleForm.pwd" type="password"></el-input>
      </el-form-item>
           <el-form-item label="年龄" prop="age">
        <el-input v-model="ruleForm.age" ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即创建</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {

mounted() {
  console.log('xxxx');
  fetch('/api/home').then(res=>res.json())
  .then(data=>{
    console.log(data);
  })

},


  data() {
    return {
      ruleForm: {
        name: "",
        pwd: "",
        age:''
      },
      rules: {
        name: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" },
        ],
        pwd: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" },
        ],
         age: [
           
          { required: true, message: "请输入用户年龄", trigger: "blur" },
          { min: 1, max: 3, message: "长度在 1 到 3 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit success!");
          // 1 登录成功 存储后台给我们的token
          let flag = true;
          localStorage.setItem("myToken", JSON.stringify(flag));
          // 2 跳转到首页

          // 登录成功了我们需要跳转到home 组件

          this.$router.push("/home");
        } else {
          alert("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.ruleForm {
  width: 600px;
  height: 400px;
  margin: 10px auto;
}
</style>
